package interviewClassic150;

import java.util.HashMap;
import java.util.Map;

public class TwoSum1 {
    public int[] twoSum(int[] nums, int target) {
        //定义一个map来记录，value为对应已遍历过的索引，key为target-nums[i]
        Map<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            //判断之前是否nums[i]的key，如果有，则直接返回
            if (map.containsKey(nums[i])) return new int[]{map.get(nums[i]),i};
            //不存在，则存入当前
            int key = target - nums[i];
            map.put(key,i);
        }
        return new int[]{-1,-1};
    }
}
